package cn.lzy.taskmanagement.AsyncHasResult;

import cn.lzy.taskmanagement.util.DataUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

/**
 * @Author: WeiKaiYe
 * @CreateTime: 2023-10-21  17:59
 */

@Service
public class ResultAsyncService {
    @Async
    public Future<Integer> processA() throws Exception {
        System.out.println(" 计算1-1000的值总和 开始执行时间====" + DataUtils.getData(DataUtils.data1));
        Long startTimeA = System.currentTimeMillis(); // 获取当前时间
        Thread.sleep(5000); // 代码等待5秒后执行

        System.out.println(" 计算1-1000的值总和 结束时间====" + DataUtils.getData(DataUtils.data1));
        int total = 0;
        for (int i = 0; i <= 1000; i++) {
            total = total + i;
        }

        System.out.println(" 1-1000计算的值总和:" + total);
        Long endTimeA = System.currentTimeMillis(); // 获取当前时间
        System.out.println(" 1-1000计算的总耗时为:" + (endTimeA - startTimeA));
        return new AsyncResult<>(total);
    }

    @Async
    public Future<Integer> processB() throws Exception {
        System.out.println(" 计算1000-2000的值总和 开始执行时间====" + DataUtils.getData(DataUtils.data1));
        Long startTimeB = System.currentTimeMillis();

        System.out.println(" 计算1000-2000的值总和 结束时间====" + DataUtils.getData(DataUtils.data1));
        int total1 = 1000;
        for (int i = 0; i <= 2000; i++) {
            total1 = total1 + i;
        }

        System.out.println(" 1000-2000计算的值总和:" + total1);
        Long endTimeB = System.currentTimeMillis();
        System.out.println(" 1000-2000计算的总耗时为:" + (endTimeB - startTimeB));
        return new AsyncResult<>(total1);
    }
}
